package com.jxgc.excel.demo;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExportExcelDemo {
    public static void main(String[] args) {
        //封装数据
        List<Map<String, String>> result = getData();
        exportExcel(result);
    }
    /**
     * 导出excel
     */
    private static void exportExcel(List<Map<String, String>> data) {
        try {
            String path = "d:\\text\\text.xlsx";
            //创建工作普   XSSFWorkbook
            // HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
            //HSSFSheet sheet = hssfWorkbook.createSheet("考勤报表");
            //SXSSf与HSSF的区别
            SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
            SXSSFSheet sheet = sxssfWorkbook.createSheet("考勤报表");   //这里得到的是sheet中替换字样
            //0行0列 的数据值是张三

            //单元格 总标题
            String totalTitle = "考勤汇总表";
            SXSSFRow roowTitle = sheet.createRow(0);
            roowTitle.createCell(0).setCellValue(totalTitle);
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));

            // 样式  1.单元格长宽高  2.单元格的颜色 3.单元格的字体大小 黑体 4.单元格内容巨左  中 右
            CellStyle cellStyle = sxssfWorkbook.createCellStyle();
            short index = 1;
            cellStyle.setFillBackgroundColor(index);  //设置背景颜色
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            cellStyle.setAlignment(HorizontalAlignment.GENERAL);//居中
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

            Font font = sxssfWorkbook.createFont();
            font.setBold(true);    //是否加粗
            font.setFontHeight((short) (400));
            cellStyle.setFont(font); //字体放在样式中

            //设置单元格值
            //设置单元格值的样式

          //  roowTitle.createCell(0).setCellValue("张三");
          //  roowTitle.createCell(1).setCellValue("手机号码");   //这是干嘛的我也不知道

            // 姓名，手机号码，考勤次数，班级，性别
            String[] title = {"姓名", "手机号码", "考勤次数", "班级", "性别"};
            int t = 0;
            SXSSFRow roow = sheet.createRow(1);
            for (int i = 0; i < title.length; i++) {
                roow.createCell(i).setCellValue(title[i]);
            }
            int rowNumber = 2;
            int startLine = 2;
            int endLine = 0;
            String isUserName = "";

            Map<String, Integer> linMap = new HashMap<>();
            for (int i = 0; i < data.size(); i++) {
                SXSSFRow roowDate = sheet.createRow(rowNumber);
                Map<String, String> map = data.get(i);
                roowDate.createCell(0).setCellValue(map.get("userName"));
                roowDate.createCell(1).setCellValue(map.get("phone"));
                roowDate.createCell(2).setCellValue(map.get("checkNumber"));
                roowDate.createCell(3).setCellValue(map.get("class"));
                roowDate.createCell(4).setCellValue(map.get("sex"));

                if (isUserName.equals(map.get("userName"))) {

                } else {

                }
                rowNumber++;
                startLine++;
                isUserName = map.get("userName");
            }

            //写exel
            FileOutputStream fileOutputStream = new FileOutputStream(path);
            sxssfWorkbook.write(fileOutputStream);
            fileOutputStream.close();
            sxssfWorkbook.dispose();     //sxssfWorkbook.close(); 应使用 sxssfWorkbook.dispose();，因为 SXSSFWorkbook 实现了 AutoCloseable 接口，dispose() 方法会在完成写操作后释放临时文件。
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

        /**
         *初始化数据的方法
         */
        private static List<Map<String, String>>getData(){
            List<Map<String, String>> list = new ArrayList<>();
            Map<String, String> map = new HashMap<>();
            map.put("userName", "张三");
            map.put("phone", "13514141411");
            map.put("checkNumber", "2");
            map.put("class", "二班");
            map.put("sex", "男");
            list.add(map);


            Map<String, String> map1 = new HashMap<>();
            map1.put("userName", "张三");
            map1.put("phone", "13514141413");
            map1.put("checkNumber", "10");
            map1.put("class", "二班");
            map1.put("sex", "男");
            list.add(map1);

            Map<String, String> map2 = new HashMap<>();
            map2.put("userName", "李四");
            map2.put("phone", "13514141412");
            map2.put("checkNumber", "10");
            map2.put("class", "三班");
            map2.put("sex", "男");
            list.add(map2);

            Map<String, String> map3 = new HashMap<>();
            map3.put("userName", "张三");
            map3.put("phone", "13514141414");
            map3.put("checkNumber", "11");
            map3.put("class", "三班");
            map3.put("sex", "男");
            list.add(map3);

            return list;
        }
    }

